package com.weng.platform.platform.api;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weng.framework.core.model.ResponseData;
import com.weng.framework.web.base.BaseController;
import com.weng.framework.web.model.request.DeleteReqVo;
import com.weng.platform.platform.entity.TeamMemberRel;
import com.weng.platform.platform.model.vo.TeamMemberRelVo;
import com.weng.platform.platform.service.ITeamMemberRelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;


/**
 * 团队成员关联 API实现
 *
 * @author weng
 * @date 2021-01-15 17:18:49
 *
 */  
@Slf4j
@RestController
@RequestMapping("/team-member-rels")
@Api(value = "团队成员关联api", tags = {"团队成员关联操作接口"})
public class TeamMemberRelApi extends BaseController {
    @Autowired
    private ITeamMemberRelService teamMemberRelService;

    /**
     * 功能描述:单个获取团队成员关联
     */
    @ApiOperation(value = "获取团队成员关联", notes = "")
    @GetMapping(value = "/{id}")
    public ResponseData get(@PathVariable("id") String id){
        TeamMemberRel teamMemberRel = teamMemberRelService.getById(id);
        return success(teamMemberRel);
    }


    /**
     * 条件 分页查询团队成员关联
     */
    @ApiOperation(value = "分页获取团队成员关联", notes = "")
    @GetMapping(value = "")
    public ResponseData list(Integer pageNo,Integer pageSize){
		IPage<TeamMemberRel> iPage = teamMemberRelService.list(pageNo, pageSize);
        return success(iPage);
    }


    /**
	 * 新增团队成员关联
	 */
	@ApiOperation(value = "新增团队成员关联", notes = "")
	@PostMapping(value = "")
	public ResponseData add(@Valid @RequestBody TeamMemberRelVo teamMemberRelReq){
		TeamMemberRel teamMemberRel = Convert.convert(TeamMemberRel.class, teamMemberRelReq);
		teamMemberRelService.add(teamMemberRel);
		return success();
	}

	/**
	 * 更新团队成员关联
	 */
	@ApiOperation(value = "更新团队成员关联", notes = "")
	@PutMapping(value = "")
	public ResponseData update(@Valid @RequestBody TeamMemberRelVo teamMemberRelReq){
		TeamMemberRel teamMemberRel = Convert.convert(TeamMemberRel.class, teamMemberRelReq);
		teamMemberRelService.update(teamMemberRel);
		return success();
	}


	/**
	 * 删除团队成员关联
	 */
	@ApiOperation(value = "ids 删除团队成员关联", notes = "可以删除多个，中间以,号分隔")
	@DeleteMapping(value = "")
	public ResponseData delete(@RequestBody DeleteReqVo deleteReq){
		String ids = deleteReq.getIds();
		if(StringUtils.isEmpty(ids)){
			return failed("删除失败,id不能为空！");
		}
		teamMemberRelService.removeByIds(Arrays.asList(ids.split(",")));
		return success(deleteReq.getIds());
	}
}
